package com.xysd.examproject.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.xysd.examproject.R;
import com.xysd.examproject.adapter.AppListAdapter;
import com.xysd.examproject.bean.AppInfo;
import com.xysd.examproject.util.Common;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * Created by donghui on 2017/5/26.
 */
@ContentView(R.layout.fragment_home)
public class HomeFragment extends Fragment{

    @ViewInject(R.id.appListview)
    private ListView appListview;

    private List<AppInfo> dataList;
    private AppListAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return x.view().inject(this, inflater, container);//绑定fragment注解
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initListview();
    }

    private void initListview() {
        dataList=Common.getInstalledApps(getActivity());
        adapter=new AppListAdapter(getActivity(),dataList);
        appListview.setAdapter(adapter);
        appListview.setOnItemClickListener(onItemClickListener);
    }

    AdapterView.OnItemClickListener onItemClickListener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent minIntent = getActivity().getPackageManager()
                    .getLaunchIntentForPackage(dataList.get(position).packageName);
            startActivity(minIntent);
        }
    };
}
